package com.ys.controller.api.hr;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.config.MenuId;
import com.ys.dto.DtreeSqlDto;
import com.ys.entity.hr.ThrOperateCompanyApply;
import com.ys.service.hr.ThrOperateCompanyApplyService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/inter/thrOperateCompanyInfo")
public class ApiInterThrOperateCompanyInfoController {
	
//	private static final Logger logger = Logger.getLogger(ApiAdminLoginController.class);
	
	@Autowired
	private CommonApiService<ThrOperateCompanyApply> commonApiService;
	@Autowired
	private ThrOperateCompanyApplyService thrOperateCompanyApplyService;
	

	@RequiresPermissions("thrOperateCompanyInfo:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(
			@ModelAttribute ThrOperateCompanyApply cond,
			Long id){
		cond = commonApiService.dataAuth(MenuId.THR_OperateCompanyApply, cond);
		
		//cond.setEffectStock(1);
		
		if (id != null) {
			cond.setId(null)
				.setOperateOrgId(id);
		}


		cond.addLeftJoin("TS_UserChange B on B.Id = A.OperatorChangeId")
				.addField("B.UserId operatorUserId")//运营人工号
				.addField("B.UserName operatorUserName")//运营人
				.addField("B.RoleName roleName")//运营人岗位
				.addField("B.DeptName deptName");//运营人部门

		cond.addLeftJoin("TS_OperateOrg O on O.Id = A.OperateOrgId")
				.addField("O.Name orgName");//机构名称
		
		cond.setOrderby("A.Id asc");
		
		return commonApiService.searchLay(cond);
	}
	
	@RequestMapping("/dtree")
    @ResponseBody
	public DtreeSqlDto dtree(Model model, HttpServletRequest request, HttpServletResponse response
			){
		return thrOperateCompanyApplyService.dtree();
	}
	
	/**
	 * 编辑
	 */
	@PostMapping("/edit")
    @ResponseBody
	public R edit(HttpServletRequest request, @RequestParam Map<String, Object> params){
		try {
			return thrOperateCompanyApplyService.edit(params);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("服务器正在开小差，请联系客服");
		}
	}
	
	/**
	 * 修改运营人
	 */
	@RequiresPermissions("thrOperateCompanyInfo:editOperator")
	@PostMapping("/editOperator")
    @ResponseBody
	public R editOperator(HttpServletRequest request,
			Long id, String cUser, String password){
		try {
			return thrOperateCompanyApplyService.editOperator(id, cUser, password);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
		}
	}
	
	/**
	 * 修改运营状态
	 */
	@RequiresPermissions("thrOperateCompanyInfo:editStatus")
	@PostMapping("/editStatus")
    @ResponseBody
	public R editStatus(HttpServletRequest request,
			Long id, Integer status, String password){
		try {
			return thrOperateCompanyApplyService.editStatus(id, status, password);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
		}
	}
	
	/**
	 * 批量变更组织机构
	 */
	@RequiresPermissions("thrOperateCompanyInfo:batchChangeOrg")
	@PostMapping("/batchChangeOrg")
    @ResponseBody
	public R batchChangeOrg(HttpServletRequest request,
			Long operateOrgId,
			String subData) throws Exception{
		
		return thrOperateCompanyApplyService.batchChangeOrg(operateOrgId, subData);
	}
}
